<?php
/**
 * Created by PhpStorm.
 * User: xutao
 * Date: 2018/8/8
 * Time: 13:43
 */
namespace app\index\controller;
use app\admin\model\MemberModel;
use think\Loader;
use think\Session;

class Member extends Base{
    //获取用户资料
    public function get_data()
    {
        if (request()->isPost()) {
            if(!Session::get('member')) return json(['code'=>9,'data'=>'','msg'=>'请登录']);
            return json(['code' => 200, 'data' => Session::get('member'), 'msg' => 'ok']);
        } else {
            return json(['code' => 0, 'data' => '', 'msg' => '非法请求']);
        }
    }
    //修改用户资料
    public function edit_member_data(){

        if(\request()->isPost()){
            if(!Session::get('member')) return json(['code'=>9,'data'=>'','msg'=>'请登录']);
            $data = input();
            $data['id']=Session::get('member')['id'];
            $member_validate = Loader::validate('MemberValidate');
            if(!$member_validate->check($data)) return json(['code'=>2,'data'=>'','msg'=>$member_validate->getError()]);
            $re = MemberModel::update_data($data);
            if($re){
                return json(['code' => 200, 'data' => $re, 'msg' => '修改成功']);
            }else{
                return json(['code' => 1, 'data' => '', 'msg' => '修改失败']);
            }
        }else{
            return json(['code' => 0, 'data' => '', 'msg' => '非法请求']);
        }
    }

    //密码修改
    public function edit_pwd(){
        if(\request()->isPost()){
            if(!Session::get('member')) return json(['code'=>9,'data'=>'','msg'=>'请登录']);
            $id = Session::get('member')['id'];
            $data = input();
            if(!input('old_pwd') || !input('new_pwd')) return json(['code'=>3,'data'=>'','msg'=>'参数错误']);
            //验证旧密码
            if(md5($data['old_pwd']) != Session::get('member')['pwd']) return json(['code'=>1,'data'=>'','msg'=>'密码验证错误']);
            $member = MemberModel::get($id);
            $member->pwd = $data['new_pwd'];
            $member->update_time = date('Y-m-d H:i:s',time());
            $re = $member->save();
            if($re){
                return json(['code' => 200, 'data' => $re, 'msg' => '修改成功']);
            }else{
                return json(['code' => 2, 'data' => '', 'msg' => '修改失败']);
            }
        }else{
            return json(['code' => 0, 'data' => '', 'msg' => '非法请求']);
        }
    }
    //获取用户信息
    public function get_member(){
        if(!Session::get('member')) return json(['code'=>9,'data'=>'','msg'=>'请登录']);
        return json(['code'=>200,'data'=>Session::get('member'),'msg'=>'ok']);
    }
}